package com.mango.community.controller;

import com.mango.community.entity.Event;
import com.mango.community.entity.Page;
import com.mango.community.entity.User;
import com.mango.community.event.EventProducer;
import com.mango.community.service.FollowService;
import com.mango.community.service.UserService;
import com.mango.community.util.CommunityConstant;
import com.mango.community.util.CommunityUtil;
import com.mango.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Author:  MangoCookieMilk
 * @Date:     2020/8/16 15:51
 *
 * 关注取关的controller
 */
@Controller
public class FollowController implements CommunityConstant {

    @Autowired
    private FollowService followService;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private UserService userService;
    @Autowired
    private EventProducer eventProducer;
    /** 关注的方法
     * @param entityType
     * @param entityId
     * @return
     */
    @PostMapping("/follow")
    @ResponseBody
    public String follow(int entityType,int entityId){
        //获取当前的登录用户
        User user = hostHolder.getUser();
        //关注
        followService.follow(user.getId(),entityType,entityId);

        //触发关注事件  .setEntityUserId(entityId); 因为目前只能关注用户所有 entityId 就是用户的id
        Event event = new Event()
                .setTopic(TOPIC_FOLLOW)
                .setUserId(hostHolder.getUser().getId())
                .setEntityType(entityType)
                .setEntityId(entityId)
                .setEntityUserId(entityId);
        eventProducer.fireEvent(event);
        return CommunityUtil.getJSONString(0,"关注成功");
    }

    /** 取消关注的方法
     * @param entityType
     * @param entityId
     * @return
     */
    @PostMapping("/unFollow")
    @ResponseBody
    public String UnFollow(int entityType,int entityId){
        //获取当前的登录用户
        User user = hostHolder.getUser();
        //取消关注
        followService.UnFollow(user.getId(),entityType,entityId);
        return CommunityUtil.getJSONString(0,"已取消关注");
    }

    /** 查询某个用户关注的人的信息
     * @param userId
     * @param page
     * @param model
     * @return
     */
    @GetMapping("/followee/{userId}")
    public String getFollowee(@PathVariable("userId")int userId, Page page, Model model){
        User user = userService.findUserById(userId);
        if(user ==null){
            throw  new RuntimeException("该用户不存在!");
        }
        model.addAttribute("user",user);
        //设置分页的信息
        page.setLimit(5);
        page.setPath("/followee/"+userId);
        page.setRows((int) followService.findFolloweeCount(userId, CommunityConstant.ENTITY_TYPE_USER));
        //获取关注的人的信息集合
        List<Map<String,Object>> userList = followService.findFollowee(userId,page.getOffset(),page.getLimit());
        if(userList !=null){
            for (Map<String, Object> map : userList) {
                //获取map中的user信息
                User u = (User) map.get("user");
                map.put("hasFollowed",hasFollowed(u.getId()));
            }
        }
        model.addAttribute("users",userList);
        return "/site/followee";
    }

    /** 查询某个用户粉丝的信息
     * @param userId
     * @param page
     * @param model
     * @return
     */
    @GetMapping("/follower/{userId}")
    public String getFollower(@PathVariable("userId")int userId, Page page, Model model){
        User user = userService.findUserById(userId);
        if(user ==null){
            throw  new RuntimeException("该用户不存在!");
        }
        model.addAttribute("user",user);
        //设置分页的信息
        page.setLimit(5);
        page.setPath("/follower/"+userId);
        page.setRows((int) followService.findFollowerCount(CommunityConstant.ENTITY_TYPE_USER,userId));
        //获取关注的人的信息集合
        List<Map<String,Object>> userList = followService.findFollower(userId,page.getOffset(),page.getLimit());
        if(userList !=null){
            for (Map<String, Object> map : userList) {
                //获取map中的user信息
                User u = (User) map.get("user");
                map.put("hasFollowed",hasFollowed(u.getId()));
            }
        }
        model.addAttribute("users",userList);
        return "/site/follower";
    }

    /** 判断当前用户对某个用户是否关注
     * @param userId
     * @return
     */
    private boolean hasFollowed(int userId){
        if(hostHolder.getUser() == null){
            //没有登录
            return false;
        }
        return followService.hasFollowed(hostHolder.getUser().getId(),CommunityConstant.ENTITY_TYPE_USER,userId);
    }
}
